package com.example.surveyapplication;

import help.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ConfirmPasswordActivity extends Activity implements OnClickListener {
	Button btnCancel;
	Button btnConfirm;
	EditText txtPassword;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_confirm_password);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		initComponents();
	}
	
	private void initComponents() {
		btnCancel = (Button)findViewById(R.id.btnCancel_ConfirmPassword);
		btnConfirm = (Button)findViewById(R.id.btnConfirm_ConfirmPassword);
		txtPassword = (EditText)findViewById(R.id.txtPassword_ConfirmPassword);
		
		btnCancel.setOnClickListener(this);
		btnConfirm.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.confirm_password, menu);
		return true;
	}

	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		case R.id.btnConfirm_ConfirmPassword:
			if (checkConfirm()) {
				intent = new Intent(this, MainSettingActivity.class);
				startActivity(intent);
				finish();
			}
			else {
				showAlert("Password is wrong!");
			}
			break;
		case R.id.btnCancel_ConfirmPassword:
			super.onBackPressed();
			break;
		default:
			break;
		}
	}
	
	private void showAlert(String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("");
		alertDialog.setMessage(message);
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				}
				});
		alertDialog.show();
	}

	
	private boolean checkConfirm() {
		String password = txtPassword.getText().toString();
		
		if (Utility.currentAccount().getPassword().equals(password)) {
			return true;
		}
		else {
			return false;
		}
	}
}
